#!/usr/bin/env bash
# Shows a run menu with calculator support.
# Deps:
#   - rofi (can easily be converted to dmenu)
#   - bc or python (either version 2 or 3) for calculations
#   - notification system setup (`notify-send` should work properly)
# Running:
#   - Save the file to anything.sh
#   - chmod +x anything.sh
#   - ./anything.sh
#   - It should show a menu with executables. Input app name or math. Enjoy!
#     If you enter math, it should show the result as a notification.
#     Otherwise should run the program you selected.
# Example input: geany, 2+2, 2-1, 10/3, 2*2=, (3^2)*10

# For alias call to work
shopt -s expand_aliases
test -f {{@@ env['HOME'] @@}}/.bash_aliases && source {{@@ env['HOME'] @@}}/.bash_aliases

# The new line is necessary to introduce a new line between system commands
# and aliases
apps="$( find ${PATH//:/ } -maxdepth 1 -executable -printf '%f\n' || ( find ${PATH//:/ } -maxdepth 1 -perm +111 -type f | sed 's|^.*/||' ) )
$( alias | awk -F '=' '{print $1}' | grep 'alias ' )"
input=$( echo "$apps" | rofi -dmenu -i )

# Run calculation
if grep -q '+\|-[[:digit:]]\|\*\|\/\|\^' <<<"$input"; then
	if [ -x "$(command -v bc)" ]; then # has bc package
		# If there is an equal sign (=) at the end, get rid of it.
		input=$(echo "$input" | sed 's/\=//g')
		# Show the result in a notification.
		notify-send "The result is: "$( echo $input | bc )
	elif [ -x "$(command -v python)" ]; then # has python package
		# If there is an equal sign (=) at the end, get rid of it.
		# Replace "^" with "**" so that it can calculate powers.
		input=$(echo "$input" | sed 's/\=//g;s/\^/**/g')
		# Show the result in a notification.
		# The `float(1)*` part is so that Python 2 returns decimal places.
		notify-send "The result is: "$( python -c "print( float(1)*$input )" )
	elif [ -x "$(command -v bash)" ]; then
		# If there is an equal sign (=) at the end, get rid of it.
		input=$(echo "$input" | sed 's/\=//g')
		notify-send "The result is: "$($(dirname $0)/bcalc/bcalc "$input")
	else
		notify-send "Please install bash."
	fi
# Run aliases
elif [[ $input == "alias "* ]]; then
	alias_name=$(echo "$input" | awk -F ' ' '{print $2}')
	{{@@ terminal_exec @@}} -e "echo \"Running alias '$alias_name'...\"; bash -i -c \"$alias_name\"; echo \"Press Ctrl+D to exit this terminal...\"; read"
# Run normal commands
else
	# Not a calculation, so run the command.
	$($input)
fi
